home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 July: Mac OS SDK / Dev.CD Jul 96 SDK / Dev.CD Jul 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc / OpenDoc Development / Debugging Support / OpenDoc Source Code / Utilities / Interfaces / TempObj.h < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-22  |  14.9 KB  |  602 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        TempObj.h
  3.  
  4.     Contains:    Template utilities for exception-safe temporary object references
  5.  
  6.     Owned by:    Jens Alfke
  7.  
  8.     Copyright:    © 1995 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Theory Of Operation:
  11.     
  12.         *** See the Tech Note "Temporary References/Objects" for full documentation.
  13.         
  14.     
  15.         TempObj<T>        T :ODObject                A pointer to a temporary object
  16.         TempRef<T>        T :ODRefCntObject        A pointer to a temporary reference
  17.         
  18.         These are simple template classes that act as a transparent wrapper around
  19.         an OpenDoc object pointer. The temp object can be used wherever a pointer
  20.         to the object would be used. When the temp object goes out of scope, the
  21.         object it wraps will be deleted (in the case of TempObj) or released (in
  22.         the case of TempRef.) This includes the case where an exception is thrown:
  23.         the wrapper is exception-safe.
  24.         
  25.         Example:
  26.         
  27.             RgnHandle r;
  28.             {
  29.                 TempODShape s = frame->GetUsedShape(ev,kODNULL);
  30.                 r = s->CopyQDRegion(ev);
  31.             }
  32.         
  33.         In this example, s is wrapped around the actual ODShape returned by
  34.         the GetUsedShape method. It is used just as an ODShape* in the next
  35.         line. In the normal course of events, the shape will be released after
  36.         GetQDRegion returns, since s goes out of scope. If GetUsedShape runs
  37.         out of memory and throws an exception, the shape will also be safely
  38.         released.
  39.         
  40.         There is a certain amount of overhead associated with the TempRef
  41.         object. However, the only safe alternative to using it would be to
  42.         wrap an exception handler around the call to GetUsedShape and release
  43.         the shape in the CATCH block (and then reraise the exception) as well
  44.         as after the ENDTRY. This ends up using significantly more code and
  45.         execution time than the TempRef object -- exception handlers are
  46.         quite expensive.
  47.  
  48.     In Progress:
  49.         
  50. */
  51.  
  52. #ifndef _TEMPOBJ_
  53. #define _TEMPOBJ_
  54.  
  55. #ifndef _EXCEPT_
  56. #include <Except.h>
  57. #endif
  58.  
  59. #ifndef SOM_ODRefCntObject_xh
  60. #include "Part.xh"
  61. #endif
  62.  
  63. #ifndef SOM_ODWindow_xh
  64. #include "Window.xh"
  65. #endif
  66.  
  67. #ifndef _ODTYPES_
  68. #include <ODTypes.h>
  69. #endif
  70.  
  71.  
  72. class TempODPtr :Destructo
  73. {
  74.     public:
  75.     TempODPtr( );
  76.     TempODPtr( void *block );
  77.     ~TempODPtr( );
  78.     operator void* ( )                {return fBlock;}
  79.     void* operator= ( void *b )        {return (fBlock = b);}
  80.     
  81.     protected:
  82.     void* fBlock;
  83.  
  84.     private: // disallow these:
  85.     TempODPtr(const TempODPtr& );
  86.     void operator=(const TempODPtr& );
  87. };
  88.  
  89. class BaseTempObj :Destructo
  90. {
  91.     public:
  92.     virtual ~BaseTempObj();
  93.     
  94.     protected:
  95.     ODObject *fObj;
  96.     BaseTempObj( );
  97.  
  98.     private: // disallow these:
  99.     BaseTempObj(const BaseTempObj& );
  100.     void operator=(const BaseTempObj& );
  101.     // Bitwise assigning one destructo to another smashes
  102.     // a list link with potentially unpleasant effects.
  103. };
  104.  
  105. class BaseTempRef :Destructo
  106. {
  107.     public:
  108.     void Release();
  109.     virtual ~BaseTempRef();
  110.     
  111.     protected:
  112.     ODRefCntObject *fObj;
  113.     BaseTempRef( );
  114.  
  115.     private: // disallow these:
  116.     BaseTempRef(const BaseTempRef& );
  117.     void operator=(const BaseTempRef& );
  118.     // Bitwise assigning one destructo to another smashes
  119.     // a list link with potentially unpleasant effects.
  120. };
  121.  
  122.  
  123. #ifdef _USE_TEMPLATES_
  124.  
  125.  
  126.     //===========================================================================
  127.     //    TempObj <T>
  128.     //===========================================================================
  129.     
  130.     
  131.     template<class T> class TempObj :public BaseTempObj
  132.     {
  133.         public:
  134.         TempObj( T* );
  135.         T* operator-> ()        {return (T*)fObj;}
  136.         operator T* ()            {return (T*)fObj;}
  137.     
  138.         T* operator= (T* t)        {fObj=t; return t;}
  139.         
  140.         T* DontDelete()            {T* temp=(T*)fObj; fObj=kODNULL; return temp;}
  141.     };
  142.     
  143.     // Implementation of the TempObj constructor:
  144.     template<class T> TempObj<T>::TempObj( T *obj )
  145.     {
  146.         fObj = obj;
  147.     }
  148.     
  149.     
  150.     //===========================================================================
  151.     //    TempRef <T>
  152.     //
  153.     //    Supports a few extra goodies:
  154.     //        Release()        Releases the object and sets the pointer to NULL.
  155.     //        DontRelease()    Sets the pointer to NULL (so the destructor will not
  156.     //                            release the object) but returns the old pointer.
  157.     //
  158.     //===========================================================================
  159.     
  160.     
  161.     template<class T> class TempRef :public BaseTempRef
  162.     {
  163.         public:
  164.         TempRef( T* );
  165.         T* operator-> ()        {return (T*)fObj;}
  166.         operator T* ()            {return (T*)fObj;}
  167.         T* operator=( T *t )    {fObj=t; return t;}
  168.         
  169.         T* DontRelease()        {T* temp=(T*)fObj; fObj=kODNULL; return temp;}
  170.     };
  171.     
  172.     
  173.     // Implementation of the TempRef constructor:
  174.     template<class T> TempRef<T>::TempRef( T *obj )
  175.     {
  176.         fObj = obj;
  177.     }
  178.  
  179.  
  180. #endif /*_USE_TEMPLATES_*/    
  181.  
  182.  
  183. //===========================================================================
  184. //    Instantiations of TempObj and TempRef. Add your own if necessary.
  185. //===========================================================================
  186.  
  187. class ODFocusSetIterator;
  188. class ODSession;
  189. class ODTypeList;
  190. class ODStorageUnitView;
  191.  
  192. class ODExtension;
  193. class ODSettingExtension;
  194.  
  195. class ODFrame;
  196. class ODPart;
  197. class ODShape;
  198. class ODTransform;
  199. class ODStorageUnit;
  200.  
  201. class ODDraft;
  202. class ODDocument;
  203. class ODContainer;
  204. class ODLink;
  205. class ODLinkSource;
  206. class ODMenuBar;
  207.  
  208. #ifdef _USE_TEMPLATES_
  209.  
  210.     typedef TempObj<ODObject>                TempODObject;
  211.     typedef TempObj<ODSession>                TempODSession;
  212.     typedef TempObj<ODTypeList>                TempODTypeList;
  213.     typedef TempObj<ODStorageUnitView>        TempODStorageUnitView;
  214.     
  215.     //typedef TempRef<ODExtension>            TempODExtension;
  216.     //typedef TempRef<ODSettingExtension>        TempODSettingExtension;
  217.  
  218.     typedef TempRef<ODRefCntObject>            TempODRefCntObject;
  219.     typedef TempRef<ODWindow>                TempODWindow;
  220.     typedef TempRef<ODFrame>                TempODFrame;
  221.     typedef TempRef<ODPart>                    TempODPart;
  222.     typedef TempRef<ODShape>                TempODShape;
  223.     typedef TempRef<ODTransform>            TempODTransform;
  224.     typedef TempRef<ODStorageUnit>            TempODStorageUnit;
  225.  
  226.     typedef TempRef<ODDraft>                TempODDraft;
  227.     typedef TempRef<ODDocument>                TempODDocument;
  228.     typedef TempRef<ODContainer>            TempODContainer;
  229.     typedef TempRef<ODLink>                    TempODLink;
  230.     typedef TempRef<ODLinkSource>            TempODLinkSource;
  231.     typedef TempRef<ODMenuBar>                TempODMenuBar;
  232.  
  233. #else /* not _USE_TEMPLATES_ */
  234.  
  235.     #ifdef __MWERKS__
  236.         // Make sure 'pragma once' mode is off so the .th files can be included
  237.         // more than once!
  238.         #pragma push
  239.         #pragma once off
  240.     #endif
  241.  
  242.     /* If not using templates, we rely on the preprocessor. A "template" for an
  243.         instantiation of TempObj is in TempObj.th, and for TempRef in TempRef.th.
  244.         Include one of these headers per instantiation. */
  245.  
  246.     #define _T_        ODObject
  247.     #define _C_        TempODObject
  248.     #include "TempObj.th"
  249.     #define _T_        ODSession
  250.     #define _C_        TempODSession
  251.     #include "TempObj.th"
  252.     #define _T_        ODTypeList
  253.     #define _C_        TempODTypeList
  254.     #include "TempObj.th"
  255.     #define _T_        ODStorageUnitView
  256.     #define _C_        TempODStorageUnitView
  257.     #include "TempObj.th"
  258.     
  259.     #define _T_        ODRefCntObject
  260.     #define _C_        TempODRefCntObject
  261.     #include "TempRef.th"
  262.     #define _T_        ODWindow
  263.     #define _C_        TempODWindow
  264.     #include "TempRef.th"
  265.     #define _T_        ODFrame
  266.     #define _C_        TempODFrame
  267.     #include "TempRef.th"
  268.     #define _T_        ODPart
  269.     #define _C_        TempODPart
  270.     #include "TempRef.th"
  271.     #define _T_        ODShape
  272.     #define _C_        TempODShape
  273.     #include "TempRef.th"
  274.     #define _T_        ODTransform
  275.     #define _C_        TempODTransform
  276.     #include "TempRef.th"
  277.     #define _T_        ODStorageUnit
  278.     #define _C_        TempODStorageUnit
  279.     #include "TempRef.th"
  280.  
  281.     #define _T_        ODDraft
  282.     #define _C_        TempODDraft
  283.     #include "TempRef.th"
  284.     #define _T_        ODDocument
  285.     #define _C_        TempODDocument
  286.     #include "TempRef.th"
  287.     #define _T_        ODContainer
  288.     #define _C_        TempODContainer
  289.     #include "TempRef.th"
  290.     #define _T_        ODLink
  291.     #define _C_        TempODLink
  292.     #include "TempRef.th"
  293.     #define _T_        ODLinkSource
  294.     #define _C_        TempODLinkSource
  295.     #include "TempRef.th"
  296.     #define _T_        ODMenuBar
  297.     #define _C_        TempODMenuBar
  298.     #include "TempRef.th"
  299.  
  300. /*
  301.     #define _T_        ODExtension
  302.     #define _C_        TempODExtension
  303.     #include "TempRef.th"
  304.     #define _T_        ODSettingsExtension
  305.     #define _C_        TempODSettingsExtension
  306.     #include "TempRef.th"
  307. */
  308.     #ifdef __MWERKS__
  309.         #pragma pop
  310.     #endif
  311.  
  312. #endif /*_USE_TEMPLATES_*/
  313.  
  314.  
  315. //===========================================================================
  316. //    Temp strings.
  317. //===========================================================================
  318.  
  319. class TempODString :Destructo
  320. {
  321.     protected:
  322.     char* fStr; 
  323.  
  324.     public:
  325.     ~TempODString();
  326.     TempODString( char* );
  327.     operator char* ()         { return fStr; }
  328.  
  329.     char* operator= (char* s) { fStr = s; return s; }
  330.     
  331.     char* DontDelete()        { char* s = fStr; fStr = kODNULL; return s;}
  332.  
  333.     private: // disallow these:
  334.     TempODString( );
  335.     TempODString(const TempODString& );
  336.     void operator=(const TempODString& );
  337. };
  338.  
  339. // typedef string  ODISOStr;
  340.     // typedef ODISOStr ODContainerType;
  341.     // typedef ODISOStr  ODPropertyName;
  342.     // typedef ODISOStr  ODStorageUnitName;
  343.     // typedef    ODISOStr    ODDraftName;
  344.  
  345.     // typedef ODISOStr  ODType;
  346.         // typedef ODType    ODValueType;
  347.         // typedef ODType   ODObjectType;
  348.         // typedef ODType   ODFocusType;
  349.  
  350. // typedef char* ODEditor;
  351.  
  352.  
  353. typedef TempODString TempODISOStr;
  354. typedef TempODString TempODType;
  355. typedef TempODString TempODValueType;
  356. typedef TempODString TempODObjectType;
  357. typedef TempODString TempODFocusType;
  358. typedef TempODString TempODContainerType;
  359. typedef TempODString TempODPropertyName;
  360. typedef TempODString TempODStorageUnitName;
  361. typedef TempODString TempODDraftName;
  362. typedef TempODString TempStringPtr;
  363.  
  364. typedef TempODString TempODEditor;
  365.  
  366.  
  367. //===========================================================================
  368. //    Temp handles.
  369. //===========================================================================
  370.  
  371. class TempODHandle :Destructo
  372. {
  373.     protected:
  374.     ODHandle fHandle; 
  375.  
  376.     public:
  377.     ~TempODHandle();
  378.     TempODHandle( ODHandle );
  379.     operator ODHandle ()           { return fHandle; }
  380.  
  381.     ODHandle operator= (ODHandle h)
  382.     { fHandle = h; return h; }
  383.     
  384.     ODHandle DontDelete()
  385.     { ODHandle h = fHandle; fHandle = kODNULL; return h;}
  386.  
  387.     private: // disallow these:
  388.     TempODHandle( );
  389.     TempODHandle(const TempODHandle& );
  390.     void operator=(const TempODHandle& );
  391. };
  392.  
  393. class TempODHandleLock :Destructo
  394. {
  395.     protected:
  396.     ODHandle fHandle; 
  397.  
  398.     public:
  399.     ~TempODHandleLock();            // unlocks the handle
  400.     TempODHandleLock( ODHandle );   // locks the handle
  401.     operator ODHandle ()           { return fHandle; }
  402.  
  403.     ODHandle operator= (ODHandle h)
  404.     { fHandle = h; return h; }
  405.  
  406.     private: // disallow these:
  407.     TempODHandleLock( );
  408.     TempODHandleLock(const TempODHandleLock& );
  409.     void operator=(const TempODHandleLock& );
  410. };
  411.  
  412. //===========================================================================
  413. //    Temp platform files.
  414. //===========================================================================
  415.  
  416. class PlatformFile;
  417.  
  418. class TempPlatformFile :Destructo
  419. {
  420.     protected:
  421.     PlatformFile* fFile; 
  422.  
  423.     public:
  424.     ~TempPlatformFile();
  425.     TempPlatformFile( PlatformFile* );
  426.     PlatformFile* operator-> ()         { return fFile; }
  427.     operator PlatformFile* ()           { return fFile; }
  428.  
  429.     PlatformFile* operator= (PlatformFile* f)
  430.     { fFile = f; return f; }
  431.     
  432.     PlatformFile* DontDelete()
  433.     { PlatformFile* f = fFile; fFile = kODNULL; return f;}
  434.  
  435.     private: // disallow these:
  436.     TempPlatformFile( );
  437.     TempPlatformFile(const TempPlatformFile& );
  438.     void operator=(const TempPlatformFile& );
  439. };
  440.  
  441. //===========================================================================
  442. //    Temp ODPasteAsResult.
  443. //===========================================================================
  444.  
  445. // struct ODPasteAsResult; // from ODTypesM.xh
  446.  
  447. class TempODPasteAsResult :Destructo
  448. {
  449.     protected:
  450.     ODPasteAsResult* fResult; 
  451.  
  452.     public:
  453.     ~TempODPasteAsResult();
  454.     TempODPasteAsResult( ODPasteAsResult* );
  455.     ODPasteAsResult* operator-> ()         { return fResult; }
  456.     operator ODPasteAsResult* ()           { return fResult; }
  457.  
  458.     ODPasteAsResult* operator= (ODPasteAsResult* r)
  459.     { fResult = r; return r; }
  460.     
  461.     ODPasteAsResult* DontDelete()
  462.     { ODPasteAsResult* r = fResult; fResult = kODNULL; return r;}
  463.  
  464.     private: // disallow these:
  465.     TempODPasteAsResult( );
  466.     TempODPasteAsResult(const TempODPasteAsResult& );
  467.     void operator=(const TempODPasteAsResult& );
  468. };
  469.  
  470.  
  471. //===========================================================================
  472. //    Temp AEDescs.
  473. //===========================================================================
  474.  
  475. struct AEDesc;
  476.  
  477. class TempAEDesc :Destructo
  478. {
  479.     protected:
  480.     AEDesc* fDesc; 
  481.  
  482.     public:
  483.     ~TempAEDesc();
  484.     TempAEDesc( AEDesc* );
  485.     AEDesc* operator-> ()         { return fDesc; }
  486.     operator AEDesc* ()           { return fDesc; }
  487.  
  488.     AEDesc* operator= (AEDesc* d)
  489.     { fDesc = d; return d; }
  490.     
  491.     AEDesc* DontDelete()
  492.     { AEDesc* d = fDesc; fDesc = kODNULL; return d;}
  493.  
  494.     private: // disallow these:
  495.     TempAEDesc( );
  496.     TempAEDesc(const TempAEDesc& );
  497.     void operator=(const TempAEDesc& );
  498. };
  499.  
  500.  
  501. //===========================================================================
  502. //    Temp ODIText.
  503. //===========================================================================
  504.  
  505. class ODIText;
  506.  
  507. class TempODIText :Destructo
  508. {
  509.     protected:
  510.     ODIText* fText; 
  511.  
  512.     public:
  513.     ~TempODIText();
  514.     TempODIText( ODIText* );
  515.     ODIText* operator-> ()          { return fText; }
  516.     operator ODIText* ()            { return fText; }
  517.  
  518.     ODIText* operator= (ODIText* t)
  519.     { fText = t; return t; }
  520.     
  521.     ODIText* DontDelete()
  522.     { ODIText* t = fText; fText = kODNULL; return t;}
  523.  
  524.     private: // disallow these:
  525.     TempODIText( );
  526.     TempODIText(const TempODIText& );
  527.     void operator=(const TempODIText& );
  528. };
  529.  
  530. // typedef ODIText ODName; 
  531. // typedef ODName  ODDocumentName;
  532.  
  533. typedef TempODIText TempODName;
  534. typedef TempODIText TempODDocumentName;
  535.  
  536. //===========================================================================
  537. //    Temp byte arrays.
  538. //===========================================================================
  539.  
  540. class TempODByteArray :Destructo
  541. {
  542.     protected:
  543.     ODByteArray* fBA; 
  544.  
  545.     public:
  546.     ~TempODByteArray();
  547.     TempODByteArray( ODByteArray* );
  548.     ODByteArray* operator-> ()          { return fBA; }
  549.     operator ODByteArray* ()            { return fBA; }
  550.  
  551.     ODByteArray* operator= (ODByteArray* ba) 
  552.     { fBA = ba; return ba; }
  553.     
  554.     ODByteArray* DontDelete()           
  555.     { ODByteArray* ba = fBA; fBA = kODNULL; return ba;}
  556.  
  557.     private: // disallow these:
  558.     TempODByteArray( );
  559.     TempODByteArray(const TempODByteArray& );
  560.     void operator=(const TempODByteArray& );
  561. };
  562.  
  563. class TempODByteArrayStruct :Destructo
  564. {
  565.     protected:
  566.     ODByteArray fBA; 
  567.  
  568.     public:
  569.     ~TempODByteArrayStruct();
  570.     TempODByteArrayStruct( );
  571.     TempODByteArrayStruct( ODByteArray ba );
  572.     ODByteArray* operator-> ()          { return &fBA; }
  573.     operator ODByteArray* ()            { return &fBA; }
  574.  
  575.     ODByteArray operator= ( ODByteArray ba ) 
  576.     { fBA = ba; return ba; }
  577.     
  578.     void DontDelete()           
  579.     { fBA._buffer = kODNULL;}
  580.  
  581.     private: // disallow these:
  582.     TempODByteArrayStruct(const TempODByteArrayStruct& );
  583.     void operator=(const TempODByteArrayStruct& );
  584. };
  585.  
  586. // typedef ODByteArray ODContainerID;
  587. // typedef ODByteArray ODActionData;
  588. // typedef ODByteArray ODContour;
  589. // typedef ODByteArray ODPolygon;
  590.  
  591. typedef TempODByteArray TempODContainerID;
  592. typedef TempODByteArray TempODActionData;
  593. typedef TempODByteArray TempODContour;
  594. // typedef TempODByteArray TempODPolygon; // use ODTempPolygon instead
  595.  
  596. typedef TempODByteArrayStruct TempODContainerIDStruct;
  597. typedef TempODByteArrayStruct TempODActionDataStruct;
  598. typedef TempODByteArrayStruct TempODContourStruct;
  599. // typedef TempODByteArrayStruct TempODPolygonStruct;
  600.  
  601. #endif /*_TEMPOBJ_*/
  602.